package com.atguigu.tingshu.user.strategy.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.model.user.UserVipService;
import com.atguigu.tingshu.model.user.VipServiceConfig;
import com.atguigu.tingshu.user.mapper.UserInfoMapper;
import com.atguigu.tingshu.user.mapper.UserVipServiceMapper;
import com.atguigu.tingshu.user.mapper.VipServiceConfigMapper;
import com.atguigu.tingshu.user.strategy.PaidRecordStrategy;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author: atguigu
 * @create: 2025-03-25 09:08
 */
@Slf4j
@Component("1003") //默认Bean对象ID=类名(首首字母小写)
public class VIPPaidRecordStrategy implements PaidRecordStrategy {

    @Autowired
    private UserVipServiceMapper userVipServiceMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private VipServiceConfigMapper vipServiceConfigMapper;

    /**
     * 处理VIP虚拟物品发货逻辑
     * @param userPaidRecordVo
     */
    @Override
    public void handlerPaidRecord(UserPaidRecordVo userPaidRecordVo) {
        log.info("处理VIP虚拟物品发货逻辑");
        //4.处理付款项目类型为：VIP套餐 新增套餐购买记录 更新 会员标识及过期时间
        //4.1 判断订单编号是否已处理
        Long count = userVipServiceMapper.selectCount(
                new LambdaQueryWrapper<UserVipService>()
                        .eq(UserVipService::getOrderNo, userPaidRecordVo.getOrderNo())
        );
        if (count > 0) {
            return;
        }
        //4.2 根据用户ID查询用户信息 判断是否为VIP
        UserInfo userInfo = userInfoMapper.selectById(userPaidRecordVo.getUserId());
        Boolean isVIP = false;
        if (userInfo.getIsVip().intValue() == 1 && userInfo.getVipExpireTime().after(new Date())) {
            isVIP = true;
        }

        //4.3 根据套餐ID查询套餐信息 得到套餐服务月数
        VipServiceConfig vipServiceConfig = vipServiceConfigMapper.selectById(userPaidRecordVo.getItemIdList().get(0));
        Integer serviceMonth = vipServiceConfig.getServiceMonth();

        //4.4 封装会员购买套餐记录并且保存 计算会员生效时间跟过期时间
        UserVipService userVipService = new UserVipService();
        //4.4.1 如果是新开会员，本次会员购买记录生效时间=当前系统时间，过期时间=当前系统时间+套餐服务月数
        if (!isVIP) {
            userVipService.setStartTime(new Date());
            DateTime expireTime = DateUtil.offsetMonth(new Date(), serviceMonth);
            userVipService.setExpireTime(expireTime);
        } else {
            //4.4.2 如果是续费会员，本次会员购买记录生效时间=现有会员过期时间+1天   过期时间：现有会员过期时间+1天+服务月数
            userVipService.setStartTime(DateUtil.offsetDay(userInfo.getVipExpireTime(), 1));
            userVipService.setExpireTime(DateUtil.offsetMonth(userVipService.getStartTime(), serviceMonth));
        }
        //4.4.3 设置其他属性
        userVipService.setOrderNo(userPaidRecordVo.getOrderNo());
        userVipService.setUserId(userPaidRecordVo.getUserId());
        userVipServiceMapper.insert(userVipService);

        //4.5 更新用户表中会员标识以及过期时间
        userInfo.setIsVip(1);
        userInfo.setVipExpireTime(userVipService.getExpireTime());
        userInfoMapper.updateById(userInfo);
    }
}
